package cn.rdq.iknessplatform.task;

import cn.rdq.api.po.Views;
import cn.rdq.common.constant.SysCurrentInfo;
import cn.rdq.dao.ViewsMapper;
import cn.rdq.iknessplatform.service.ViewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;

@Configuration
@EnableScheduling
public class ViewsScheduleTask implements SchedulingConfigurer {

    //        private final String cron = "0 * * * * ?";    //每分钟执行一次
    private final String cron = "0 0 0 * * ?";  //每天0:00:00执行

    @Autowired
    private ViewsService viewsService;

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addTriggerTask(() -> {

//            System.out.println("--------------每分钟执行一次:" + new Date() + "-----------");
            Views views = new Views();
            views.setViews(SysCurrentInfo.viewsToday);
            views.setDateHistory(new Date());
            viewsService.saveViews(views);
            SysCurrentInfo.resetViewsToday();
            SysCurrentInfo.resetOnline();
        }, triggerContext -> new CronTrigger(cron).nextExecutionTime(triggerContext));
    }
}
